package com.sdb.clinic.service.impl;

import java.util.List;
import java.util.Random;

import com.sdb.common.utils.DateUtils;
import com.sdb.common.utils.SecurityUtils;
import com.sdb.medicalrecord.service.ITdMedicalrecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sdb.clinic.mapper.TdRegisterMapper;
import com.sdb.clinic.domain.TdRegister;
import com.sdb.clinic.service.ITdRegisterService;
import com.sdb.schedule.service.ITdScheduleService;

/**
 * 挂号Service业务层处理
 * 
 * @author yyh
 * @date 2025-01-08
 */
@Service
public class TdRegisterServiceImpl implements ITdRegisterService 
{
    @Autowired
    private TdRegisterMapper tdRegisterMapper;

    @Autowired
    private ITdScheduleService tdScheduleService;

    @Autowired
    ITdMedicalrecordService tdMedicalrecordService;


    private static final int REGISTER_ID_LENGTH = 8;
    private static final String PREFIX = "C"; // 就诊 ID 前缀
    private Random random = new Random();

    private synchronized void generateUniqueClinicId(TdRegister tdRegister) {
        String clinicId;
        do {
            clinicId = PREFIX + generateRandomClinicId();
        } while (isClinicIdExists(clinicId));

        tdRegister.setClinicId(clinicId);
    }

    private String generateRandomClinicId() {
        long num = Math.abs(random.nextLong()) % 100000000L; // 生成一个8位的随机数
        return String.format("%08d", num); // 确保有前导零
    }

    private boolean isClinicIdExists(String clinicId) {
        return tdRegisterMapper.selectByClinicId(clinicId) != null;
    }

    /**
     * 查询挂号
     * 
     * @param registerId 挂号主键
     * @return 挂号
     */
    @Override
    public TdRegister selectTdRegisterByRegisterId(Long registerId)
    {
        return tdRegisterMapper.selectTdRegisterByRegisterId(registerId);
    }

    /**
     * 查询挂号列表
     * 
     * @param tdRegister 挂号
     * @return 挂号
     */
    @Override
    public List<TdRegister> selectTdRegisterList(TdRegister tdRegister)
    {
        return tdRegisterMapper.selectTdRegisterList(tdRegister);
    }

    /**
     * 查询今日挂号列表
     *
     * @param tdRegister 挂号
     * @return 挂号
     */
    @Override
    public List<TdRegister> selectTodayTdRegisterList(TdRegister tdRegister)
    {
        return tdRegisterMapper.selectTodayTdRegisterList(tdRegister);
    }

    /**
     * 查询历史挂号列表
     *
     * @param tdRegister 挂号
     * @return 挂号
     */
    @Override
    public List<TdRegister> selectHistoryTdRegisterList(TdRegister tdRegister)
    {
        return tdRegisterMapper.selectHistoryTdRegisterList(tdRegister);
    }

    /**
     * 待发药患者列表
     *
     * @param tdRegister 挂号
     * @return 发药患者
     */
    @Override
    public List<TdRegister> selectWaitTdRegisterList(TdRegister tdRegister)
    {
        return tdRegisterMapper.selectWaitTdRegisterList(tdRegister);
    }

    /**
     * 新增挂号
     * 
     * @param tdRegister 挂号
     * @return 结果
     */
    @Override
    public int insertTdRegister(TdRegister tdRegister)
    {
        tdRegister.setCreateTime(DateUtils.getNowDate());
        tdRegister.setType(1L);
        tdRegister.setStatus(1L);
        generateUniqueClinicId(tdRegister);
        int scheduleId = Integer.parseInt(tdRegister.getScheduleId());
        tdScheduleService.register(scheduleId);
        String clinicId = tdRegister.getClinicId();
        tdMedicalrecordService.insertClinicId(clinicId);
        return tdRegisterMapper.insertTdRegister(tdRegister);
    }

    /**
     * 预约挂号
     *
     * @param tdRegister 预约挂号
     * @return 结果
     */
    @Override
    public int insertBookTdRegister(TdRegister tdRegister)
    {
        tdRegister.setCreateTime(DateUtils.getNowDate());
        tdRegister.setType(2L);
        tdRegister.setStatus(1L);
        generateUniqueClinicId(tdRegister);
        int scheduleId = Integer.parseInt(tdRegister.getScheduleId());
        tdScheduleService.register(scheduleId);
        return tdRegisterMapper.insertTdRegister(tdRegister);
    }

    /**
     * 修改挂号
     * 
     * @param tdRegister 挂号
     * @return 结果
     */
    @Override
    public int updateTdRegister(TdRegister tdRegister)
    {
        return tdRegisterMapper.updateTdRegister(tdRegister);
    }

    /**
     * 批量删除挂号
     * 
     * @param registerIds 需要删除的挂号主键
     * @return 结果
     */
    @Override
    public int deleteTdRegisterByRegisterIds(Long[] registerIds)
    {
        return tdRegisterMapper.deleteTdRegisterByRegisterIds(registerIds);
    }

    /**
     * 删除挂号信息
     * 
     * @param registerId 挂号主键
     * @return 结果
     */
    @Override
    public int deleteTdRegisterByRegisterId(Long registerId)
    {
        return tdRegisterMapper.deleteTdRegisterByRegisterId(registerId);
    }

    /**
     * 更新挂号状态
     *
     * @param clinicId 就诊号
     * @return 结果
     */
    @Override
    public int updateStatus(String clinicId){
        return tdRegisterMapper.updateStatus(clinicId);
    }

    @Override
    public int insertPersonTdRegister(TdRegister tdRegister) {
        tdRegister.setCreateTime(DateUtils.getNowDate());
        tdRegister.setType(1L);
        tdRegister.setStatus(1L);
        generateUniqueClinicId(tdRegister);
        int scheduleId = Integer.parseInt(tdRegister.getScheduleId());
        tdScheduleService.register(scheduleId);
        String clinicId = tdRegister.getClinicId();
        tdMedicalrecordService.insertClinicId(clinicId);
        return tdRegisterMapper.insertTdRegister(tdRegister);
    }

    @Override
    public List<TdRegister> listUserRegister(TdRegister tdRegister) {
        Long userid = SecurityUtils.getUserId();
        tdRegister.setCardId(tdRegisterMapper.getcardId(userid));
        return tdRegisterMapper.listUserRegister(tdRegister);
    }
}
